<template>
  <div>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix box-header">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <el-table :data="cateList" border stripe>
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="{ row }">
            <el-button type="primary" size="mini" @click="showUpDate(row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addVisible"
      width="30%"
      :close-on-click-modal="false"
      @closed="onaddClosed"
    >
      <el-form ref="addFromRef" :rules="addFromRules" :model="addFrom" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addFrom.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addFrom.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNewCate">填 加</el-button>
      </span>
    </el-dialog>
    <!-- 修改的对话框 -->
    <el-dialog title="修改文章" :visible.sync="editVisible" width="30%" :close-on-click-modal="false">
      <el-form ref="editFromRef" :model="editFrom" :rules="addFromRules" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editFrom.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editFrom.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="upDateCate" size="mini">修 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateListApi, addCateApi, upDateCateApi, deleteCateApi } from '@/api/article.js'
export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      addVisible: false,
      addFrom: {
        cate_name: '',
        cate_alias: ''
      },
      addFromRules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须1-10位的非空字符' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须1-15位的字母或数字' }
        ]
      },
      // 修改框显示和隐藏
      editVisible: false,
      editFrom: {
        id: '',
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  created() {
    this.initCateList()
  },
  methods: {
    async initCateList() {
      const { data: res } = await getCateListApi()
      if (res.code !== 0) return this.$message.error('获取文章列表失败')
      this.cateList = res.data
    },
    onaddClosed() {
      this.$refs.addFromRef.resetFields()
    },
    addNewCate() {
      this.$refs.addFromRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCateApi(this.addFrom)
        if (res.code !== 0) return this.$message.error('添加文章失败')
        this.$message.success(res.message)
        this.addVisible = false
        this.initCateList()
      })
    },
    showUpDate(row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许删除这篇文章')

      this.editFrom = Object.assign({}, row)
      this.editVisible = true
    },
    upDateCate() {
      this.$refs.editFromRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await upDateCateApi(this.editFrom)
        if (res.code !== 0) return this.$message.error('修改文章失败')
        this.$message.success(res.message)
        this.editVisible = false
        this.initCateList()
      })
    },
    async remove(id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除此分类')
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      const { data: res } = await deleteCateApi(id)
      if (res.code !== 0) return this.$message.error('删除此分类失败')
      this.$message.success(res.message)
      this.initCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.box-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
